package com.atguigu.windows;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.Date;

public class Flink05_ProcessFunction {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        env.setParallelism(2);

        //实验一下窗口的增量聚合函数
        env.socketTextStream("hadoop162",9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] datas = value.split(",");
                        return new WaterSensor(datas[0],Long.valueOf(datas[1]),Integer.valueOf(datas[2]));
                    }
                })
                .keyBy(new KeySelector<WaterSensor, String>() {
                    @Override
                    public String getKey(WaterSensor value) throws Exception {
                        return value.getId();
                    }
                })
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<WaterSensor>() {
                    @Override
                    public WaterSensor reduce(WaterSensor value1, WaterSensor value2) throws Exception {
                        System.out.println("Flink05_ProcessFunction.reduce");

                        value1.setVc(value1.getVc()+value2.getVc());
                        return value1;
                    }


                },//由于只使用ReduceFunction方法的话就只会处理结果值，不能得到上下文的具体信息，所以这里传入new WindowFunction参数
                        new WindowFunction<WaterSensor, WaterSensor, String, TimeWindow>() {
                            @Override
                            public void apply(String key,   //分组key
                                              TimeWindow window,     //窗口语义
                                              Iterable<WaterSensor> input,     //输出值，这里只有一个值，但是也是用Iterable封装起来的
                                              Collector<WaterSensor> out) throws Exception {
                                System.out.println("Flink05_ProcessFunction.apply");

                                String start = new Date(window.getStart()).toLocaleString();
                                String end = new Date(window.getEnd()).toLocaleString();
                                System.out.println("窗口开始时间："+start+",结束时间："+end);

                                //内容打印输出
                                out.collect(input.iterator().next());
                            }
                        }
                        )
                .print();



        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
